package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.plugins.entity.ExamQuestions;
import cn.jdy.plugins.entity.PptList;
import cn.jdy.plugins.service.PptListService;

/**
 * @title  ppt列表控制累 
 *  @author jga
 * @date 2017-11-09
 */
@Controller
@RequestMapping("/pptList")
public class PptListCtrl extends BaseCtrl {
	@Autowired
	private PptListService pptListService;
	/**
	 * 初始化
	 * 
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(required = false) Integer s,PptList obj) {
		ModelAndView mv = new ModelAndView("/pptList/pptListList.jsp");
		if (s != null)
			switch (s) {
			case 1:
				mv.addObject("msg", "保存成功！");
				break;
			case 2:
				mv.addObject("msg", "更新成功！");
				break;
			case 3:
				mv.addObject("msg", "删除成功！");
				break;
			case 4:
				mv.addObject("msg", "操作失败！");
				break;
			default:
				break;
			}
		mv.addObject("data",obj);

		return mv;
	}
	
	
	/**
	 * 异步查询
	 * @param visitLog
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(PptList obj,String fromDate, String toDate) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", pptListService.query(obj,fromDate,toDate));
			jsonMap.put("iTotalDisplayRecords", pptListService.count(obj,fromDate,toDate));
			jsonMap.put("iTotalRecords", pptListService.count());
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}

	
	/**
	 * 删除
	 * @return
	 */
	@RequestMapping("/del/{id}")
	@ResponseBody
	public Map<String, Object> del(@PathVariable Long id) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			this.pptListService.delete(id);
			jsonMap.put("msg", "成功");

		} catch(Exception e) {
			jsonMap.put("dataList", "失败");
			BaseLog.e(getClass(), "删除异常！", e);
		}
		return jsonMap;
	}
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(PptList obj) {
		ModelAndView mv = new ModelAndView("/pptList/pptListDetail.jsp");
		mv.addObject("liveId", obj.getLiveId());
		mv.addObject("data", obj.getId()==null||obj.getId()==0 ? obj.getId() : this.pptListService.find(obj.getId()));
		return mv;
	}

	/**
	 * 保存（更新）
	 * @param Live
	 * @return
	 */
	@RequestMapping("/save")
	public ModelAndView save(PptList obj) {
		ModelAndView mv = new ModelAndView();
		try {
			if(obj.getId()==null) {
				obj.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.pptListService.save(obj);
				//mv.setViewName(this.viewName.concat("1"));
	
				mv.setViewName("redirect:/pptList/list/?liveId="+obj.getLiveId());

			} else {
				obj.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.pptListService.update(obj);
			//	mv.setViewName(this.viewName.concat("2"));
				mv.setViewName("redirect:/pptList/list/?liveId="+obj.getLiveId());

		
			}
		
		} catch(Exception e) {
			BaseLog.e(getClass(), "保存异常！", e);
		}
		return mv;
	}
	
}
